﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Collections;
using System.Drawing.Printing;
using WD.Properties;

namespace WD.Classes
{
    class PrintPenalty : System.Drawing.Printing.PrintDocument
    { 
        private PrintDocument ThePrintDocument;
		private DataTable TheTable;
		private DataGrid  TheDataGrid;

		public int RowCount = 0;  // current count of rows;
		private const int kVerticalCellLeeway = 10;
        private const int PAGE_LEFT_SPACE = 75;
        private const int COLUMNS_SHRINK = 10;
		public int PageNumber = 1;
		public ArrayList Lines = new ArrayList();

		int PageWidth;
		int PageHeight;
		int TopMargin;
		int BottomMargin;



        public PrintPenalty(DataGrid aGrid, PrintDocument aPrintDocument, DataTable aTable)
		{
			//
			// TODO: Add constructor logic here
			//
			TheDataGrid = aGrid;
			ThePrintDocument = aPrintDocument;
			TheTable = aTable;

			PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width-150;
			PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
			TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top+150;
			BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;

		}

		public void DrawHeader(Graphics g)
		{
			SolidBrush ForeBrush = new SolidBrush(TheDataGrid.HeaderForeColor);
			SolidBrush BackBrush = new SolidBrush(TheDataGrid.HeaderBackColor);
			Pen TheLinePen = new Pen(TheDataGrid.GridLineColor, 1);
			StringFormat cellformat = new StringFormat();
			cellformat.Trimming = StringTrimming.EllipsisCharacter;
			cellformat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;



            int columnwidth = PageWidth / TheTable.Columns.Count - COLUMNS_SHRINK;

			int initialRowCount = RowCount;

			// draw the table header
            float startxposition = TheDataGrid.Location.X + PAGE_LEFT_SPACE;
			RectangleF nextcellbounds = new RectangleF(0, 0, 0, 0);

			RectangleF HeaderBounds  = new RectangleF(0, 0, 0, 0);

            HeaderBounds.X = TheDataGrid.Location.X + PAGE_LEFT_SPACE;
			HeaderBounds.Y = TheDataGrid.Location.Y + TopMargin + (RowCount - initialRowCount) * (TheDataGrid.Font.SizeInPoints  + kVerticalCellLeeway);
			HeaderBounds.Height = TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway;
			HeaderBounds.Width = PageWidth;

			g.FillRectangle(BackBrush, HeaderBounds);

			for (int k = 0; k < TheTable.Columns.Count; k++)
			{
				string nextcolumn = TheTable.Columns[k].ToString();
				RectangleF cellbounds = new RectangleF(startxposition, TheDataGrid.Location.Y + TopMargin + (RowCount - initialRowCount) * (TheDataGrid.Font.SizeInPoints  + kVerticalCellLeeway),
					columnwidth, 
					TheDataGrid.HeaderFont.SizeInPoints + kVerticalCellLeeway);
				nextcellbounds = cellbounds;

				if (startxposition + columnwidth <= PageWidth)
				{
					g.DrawString(nextcolumn, TheDataGrid.HeaderFont, ForeBrush, cellbounds, cellformat);
				}

				startxposition = startxposition + columnwidth;

			}
	
			if (TheDataGrid.GridLineStyle != DataGridLineStyle.None)
				g.DrawLine(TheLinePen, TheDataGrid.Location.X, nextcellbounds.Bottom, PageWidth, nextcellbounds.Bottom);
		}

		public bool DrawRows(Graphics g)
		{
			int lastRowBottom = TopMargin;

			try
			{
                //SolidBrush ForeBrush = new SolidBrush(TheDataGrid.ForeColor);
                //SolidBrush BackBrush = new SolidBrush(TheDataGrid.BackColor);
                //SolidBrush AlternatingBackBrush = new SolidBrush(TheDataGrid.AlternatingBackColor);
                //Pen TheLinePen = new Pen(TheDataGrid.GridLineColor, 1);
                //StringFormat cellformat = new StringFormat();
                //cellformat.Trimming = StringTrimming.EllipsisCharacter;
                //cellformat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;
                //int columnwidth = PageWidth / TheTable.Columns.Count - COLUMNS_SHRINK;

                //int initialRowCount = RowCount;

                //RectangleF RowBounds  = new RectangleF(0, 0, 0, 0);

                //// draw vertical lines

                //int intRowCount = initialRowCount;
                //if (TheTable.Rows.Count > Properties.Settings.Default.PRINT_PENALTY_ROW_LIMIT)
                //{
                //    intRowCount = TheTable.Rows.Count - Properties.Settings.Default.PRINT_PENALTY_ROW_LIMIT;
                //}

                //// draw the rows of the table
                //for (int i = intRowCount; i < TheTable.Rows.Count; i++)
                //{
                //    DataRow dr = TheTable.Rows[i];
                //    int startxposition = TheDataGrid.Location.X + +PAGE_LEFT_SPACE;

                //    RowBounds.X = TheDataGrid.Location.X;
                //    RowBounds.Y = TheDataGrid.Location.Y + TopMargin + ((RowCount - initialRowCount)+1) * (TheDataGrid.Font.SizeInPoints  + kVerticalCellLeeway);
                //    RowBounds.Height = TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway;
                //    RowBounds.Width = PageWidth;
                //    Lines.Add(RowBounds.Bottom);

                //    if (i%2 == 0)
                //    {
                //        g.FillRectangle(BackBrush, RowBounds);
                //    }
                //    else
                //    {
                //        g.FillRectangle(AlternatingBackBrush, RowBounds);
                //    }


                //    for (int j = 0; j < TheTable.Columns.Count; j++)
                //    {
                //        RectangleF cellbounds = new RectangleF(startxposition, 
                //            TheDataGrid.Location.Y + TopMargin + ((RowCount - initialRowCount) + 1) * (TheDataGrid.Font.SizeInPoints  + kVerticalCellLeeway),
                //            columnwidth, 
                //            TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);
									

                //        if (startxposition + columnwidth <= PageWidth)
                //        {
                //            g.DrawString(dr[j].ToString(), TheDataGrid.Font, ForeBrush, cellbounds, cellformat);
                //            lastRowBottom = (int)cellbounds.Bottom;
                //        }

                //        startxposition = startxposition + columnwidth;
                //    }

                //    RowCount++;

                //    if (RowCount * (TheDataGrid.Font.SizeInPoints  + kVerticalCellLeeway) > (PageHeight * PageNumber) - (BottomMargin+TopMargin))
                //    {
                //        DrawHorizontalLines(g, Lines);
                //        DrawVerticalGridLines(g, TheLinePen, columnwidth, lastRowBottom);
                //        return true;
                //    }


                //}

                //DrawHorizontalLines(g, Lines);
                //DrawVerticalGridLines(g, TheLinePen, columnwidth, lastRowBottom);
                return false;

			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message.ToString() + ex.StackTrace.ToString());
				return false;
			}

		}

		void DrawHorizontalLines(Graphics g, ArrayList lines)
		{
			Pen TheLinePen = new Pen(TheDataGrid.GridLineColor, 1);

			if (TheDataGrid.GridLineStyle == DataGridLineStyle.None)
				return;

			for (int i = 0;  i < lines.Count; i++)
			{
                g.DrawLine(TheLinePen, TheDataGrid.Location.X + PAGE_LEFT_SPACE, (float)lines[i], PageWidth, (float)lines[i]);
			}
		}

		void DrawVerticalGridLines(Graphics g, Pen TheLinePen, int columnwidth, int bottom)
		{
			if (TheDataGrid.GridLineStyle == DataGridLineStyle.None)
				return;

			for (int k = 0; k < TheTable.Columns.Count; k++)
			{
                g.DrawLine(TheLinePen, TheDataGrid.Location.X + PAGE_LEFT_SPACE + k * columnwidth, 
					TheDataGrid.Location.Y + TopMargin,
                    TheDataGrid.Location.X + PAGE_LEFT_SPACE + k * columnwidth,
					bottom);
			}
		}


		public bool DrawDataGrid(Graphics g)
		  {

			try
			{
				this.DrawHeader(g);
				bool bContinue = DrawRows(g);
				return bContinue;
			}
			catch (Exception ex)
			{
              MessageBox.Show(ex.Message.ToString() + ex.StackTrace.ToString());
			  return false;
			}

		  }

        public bool DrawSummon(Graphics g)
        {

            try
            {
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString() + ex.StackTrace.ToString());
                return false;
            }

        }

    }
}
